<?php
class Article
{
	private $_Article;
	
	public function __construct()
	{
		$this->_Article = new Zend_Db_Table('article');
		

	}

    public function getAll()
    {
         $Rows = $this->_Article->fetchAll($this->_Article->select()->order('Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    
    public function getAllCombined()
    {
    	 $Rows = $this->_Article->fetchAll($this->_Article->select()->setIntegrityCheck(false)->from('article')->joinLeft('menu','article.categoryid = menu.id',array('menu_name'=>'menu.name'))->order('article.Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }

    
    public function getByCategory($CID)
    {
         $Rows = $this->_Article->fetchAll($this->_Article->select()->where('CategoryID =?',intval($CID))->order('Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    public function Add($Title,$Text)
    {
        $Data = array('Title'=>$Title,'Text'=>$Text); 
        $Row = $this->_Article->createRow($Data);
        
        $Row->Date = new Zend_Db_Expr('NOW()');
        $Row->save();
        return  $Row->ID;
    }

    public function Edit($ID,$Title = '',$Text = '',$CategoryID = 0,$Lang = 'ru',$HeadTitle,$Keywords,$Description)
    {
        $Row = $this->_Article->find($ID)->current();
        $Row->Text  = $Text;
        $Row->Title = $Title;
        $Row->Lang  = $Lang;
        
        $Row->HeadTitle   = $HeadTitle;    
        $Row->Keywords    = $Keywords;    
        $Row->Description = $Description;
                
        $Row->CategoryID  = $CategoryID;
        $Row->save();
    }
    
    public function Remove($ID)
    {
        $this->_Article->find($ID)->current()->delete();
    } 
    
    public function getOne($ID)
    {
        $Row = $this->_Article->find($ID)->current();
        if ($Row) return $Row->toArray();
        return false;
        
    }
}